var gnp = {
	get: function(url) {
		return this._get(url, true)
	},
	synget: function(url) {
		return this._get(url, false)
	},
	_get: function(url, syn) {
		const promise = new Promise(function(resolve, reject) {
			const handler = function() {
				if(this.readyState !== 4) {
					return;
				}
				if(this.status === 200) {
					resolve(this.response);
				} else {
					reject(new Error(this.statusText));
				}
			}
			var xhr = new XMLHttpRequest()
			xhr.open('GET', url, syn)
			xhr.onreadystatechange = handler
			xhr.send(null)
		})
		return promise
	},
	post: function(url, data) {
		return this._post(url, data, true)
	},
	synpost: function(url, data) {
		return this._post(url, data, false)
	},
	_post: function(url, data, syn) {
		const promise = new Promise(function(resolve, reject) {
			const handler = function() {
				if(this.readyState !== 4) {
					return;
				}
				if(this.status === 200) {
					resolve(this.response);
				} else {
					reject(new Error(this.statusText));
				}
			}
			const t = function(data, key = '') {
				var ret = ""
				if(typeof data == 'object') {
					for(var it in data) {
						ret += t(data[it], key + (key == '' ? it : "[" + it + "]"))
					}
				} else if(Array.isArray(data)) {
					for(var i = 0; i < data.length; i++) {
						ret += t(data[i], key + "[" + i + "]")
					}
				} else if(typeof data == 'function') {
				} else {
					ret += encodeURIComponent(key) + '=' + encodeURIComponent(data) + "&"
				}
				return ret
			}
			
			var xhr = new XMLHttpRequest()
			xhr.open('POST', url, syn)
			var sdata = {}
			if(typeof data == 'object'){
				xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded")
				var ret = t(data)
				sdata = ret.substr(0, ret.length - 1)
			}else{
				sdata = data
			}
			
			xhr.onreadystatechange = handler
			
			xhr.send(sdata)
		})
		return promise
	}
}


var guids = []

function getGuid(){
	var rnd = Math.round(Math.random() * 10000)
	if(guids.indexOf(rnd) == -1){
		guids.push(rnd)
		return rnd			
	}else{
		return getGuid()
	}
}
